<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../css/index.css">
        <title>Syba - Administration</title>
        <script type="text/javascript" src="../scripts_js/XMLHttpRequest.js"></script>
        <script type="text/javascript" src="../scripts_js/fonctions.js"></script>
    </head>
    <body onload="verification_session();recupererListeEleves();
            addId();
            recupererListeElevesN();
            cacher();">
        <header>
            <a href="../../index.html">
                <img id="home_direction" src="../images/home.png" />
            </a>
            <h1 id="title"> Gestion des Elèves </h1>
            <div id="connecté">
                Bienvenue   <b id="aff_pseudo2"></b> <input type="button" id="b_deconnexion" value="deconnexion " onclick="deconnexion();"/>
            </div>
        </header>

        <nav>
            <div id="menu">
                <input type="hidden" id="id_adulte" name="id_adulte" />
                <ul>
                    <li style="width: 20%; height: 20%;">
                        <div>
                            <p>
                                <a href="../partie_generateur/ens_gestion_eleves_ajout.html">
                                    <img src="../images/add.png" alt="Eleves" style="width: 20%; height: 20%;" />
                                </a>
                            </p>
                            <p>Ajouter un élève</p>
                        </div>
                    </li>




                </ul>
                <table align ="center">
                    <tr>
                        <th>
                            <input id ="search" type="search" placeholder="Entrez un nom" name ="Rechercher"  > </th> <th><button type="submit" onclick="rechercher();
            rechercher2();" >Rechercher </button> </th>
                    </tr>
                </table>

            </div>

            <div style="clear:both;"></div>
        </nav>
        <article>
            <table id ="cont" align ="center"> 
                <tr>
                    <td>
                        <div id="formulaire_edition_eleve1">
                            <h2 align ="center"> Mes élèves : </h2>
                            <div align="center"> 

                                <table border="1" id="tabEleves" style="text-align: center;">
                                    <tr style="font-size: 24px;">
                                        <th>
                                            NOM
                                        </th>
                                        <th>
                                            PRENOM
                                        </th>
                                        <th>
                                            Modifier
                                        </th>
                                        <th>
                                            Supprimer
                                        </th>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </td>
                    <td> 
                        <div id="autres_elèves">
                            <h2 align ="center"> Autres élèves : </h2>
                            <div align="center"> 
                                <table border="1" id="tabElevesA">
                                    <tr style="font-size: 24px;">
                                        <th>
                                            NOM
                                        </th>
                                        <th>
                                            PRENOM
                                        </th>
                                        <th>
                                            AJOUTER A MA LISTE
                                        </th>
                                    </tr>
                                </table>
                            </div>
                        </div>

                    </td>
                </tr>
            </table>

        </article>
        <footer>

        </footer>
        <script type="text/javascript">

        function cacher() {
            if (readCookie("Syba_connexion") !== null) {

                document.getElementById('aff_pseudo2').innerHTML = readCookie("Syba_connexion");

            }
            else {
                document.getElementById('connecté').setAttribute('style', 'display:none;');
            }
        }
        function deconnexion() {
            eraseCookie("Syba_connexion");
            eraseCookie("Syba_id_personne");
            window.location = "../index.html";

        }

        function addId() {
            //alert(readCookie("Syba_id_personne"));
            document.getElementById("id_adulte").value = readCookie("Syba_id_personne");
        }

        function rechercher() {
            // alert ('R1');
            //alert (document.getElementById("search").value);

            var listeDesEleves; // = recupererListeEleves() ;

            var xhr = getXMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                    alert("[" + xhr.responseText + "]");
                    //callback(xhr.responseText);
                    var docXML = xhr.responseText;
                    listeDesEleves = stringToXML(docXML);
                    construireTableauElevesR(listeDesEleves);
                }
            };

            var fd = new FormData();
            fd.append("id", readCookie("Syba_id_personne"));
            fd.append("nom", document.getElementById("search").value);

            xhr.open("POST", "../scripts_php/eleve_rechercher_tous_mes_mysql.php", false);
            xhr.send(fd);
        }

        function construireTableauElevesR(xml) {
            //
            var tab = document.getElementById("tabEleves");
            var listeDEleves = xml.getElementsByTagName("eleveR");
            // var lignes = tab.getElementsByTagName("tr");
            var enfant = tab.childNodes;
            var nbe = enfant.length;
            var n = nbe - 1;
            // alert (n);
            for (var j = n; j > 1; j--)
            {
                // alert (n);
                //tab.deleteRow(j);
                tab.removeChild(enfant[j]);
            }

            //alert ("fin",n);
            /*  if (lignes.length === 2 ){
             
             tab.deleteRow(1);
             
             }
             else {
             
             for (var i=1 ;i<= lignes.length;i++) {
             tab.deleteRow (i);
             
             }
             
             
             tab.deleteRow(1);
             
             } */

            // alert ('cccc');
            for (var i = 0; i < listeDEleves.length; i++) {
                //  alert ('hello');

                //  alert (listeDEleves.getElementByTagName("nom")[0].childNodes[0].nodeValue);
                //
                myNewTR = document.createElement("tr");
                myNewTR.setAttribute("class", "ligneQuestion");

                //var tdId = myNewTR.appendChild(document.createElement("td"));
                var tdNom = myNewTR.appendChild(document.createElement("td"));
                var tdPrenom = myNewTR.appendChild(document.createElement("td"));
                var tdModifier = myNewTR.appendChild(document.createElement("td"));
                var tdSupprimer = myNewTR.appendChild(document.createElement("td"));

                //tdId.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue));
                tdNom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("nom")[0].childNodes[0].nodeValue));
                tdNom.setAttribute('style', 'font-size: 20px;');
                tdPrenom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("prenom")[0].childNodes[0].nodeValue));
                tdPrenom.setAttribute('style', 'font-size: 20px;');

                identifiantGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                idGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                var ancreImgModifier = tdModifier.appendChild(document.createElement("a"));
                ancreImgModifier.setAttribute('href', '../partie_generateur/ens_gestion_eleves_edition.html?id=' + idGet);
                ancreImgModifier.setAttribute('onclick', 'submit()');

                var img = ancreImgModifier.appendChild(document.createElement("img"));
                img.setAttribute('src', '../images/edit.png');
                img.setAttribute('style', 'width: 50px; height: 50px;');


                var ancreImgModifier2 = tdSupprimer.appendChild(document.createElement("a"));
                ancreImgModifier2.setAttribute('href', '../partie_generateur/ens_gestion_eleves.html');
                ancreImgModifier2.setAttribute('onclick', 'supprimerEleve(' + listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue + ')');

                var img2 = ancreImgModifier2.appendChild(document.createElement("img"));
                img2.setAttribute('src', '../images/delete.png');
                img2.setAttribute('style', 'width: 50px; height: 50px;');


                tab.appendChild(myNewTR);
            }

        }

        function rechercher2() {
            //alert ('R2');
            //alert (document.getElementById("search").value);

            var listeDesEleves; // = recupererListeEleves() ;

            var xhr = getXMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                    //  alert("[" + xhr.responseText + "]");
                    //callback(xhr.responseText);
                    var docXML = xhr.responseText;
                    listeDesEleves = stringToXML(docXML);
                    construireTableauElevesNR(listeDesEleves);
                }
            };

            var fd = new FormData();
            fd.append("id", readCookie("Syba_id_personne"));
            fd.append("nom", document.getElementById("search").value);

            xhr.open("POST", "../scripts_php/eleve_rechercher_tous_Nmes_mysql.php", false);
            xhr.send(fd);
        }

        function construireTableauElevesNR(xml) {
            //

            var tab = document.getElementById("tabElevesA");
            var listeDEleves = xml.getElementsByTagName("eleveRA");

            var enfant = tab.childNodes;
            var nbe = enfant.length;
            var n = nbe - 1;
            // alert (n);
            for (var j = n; j > 1; j--)
            {
                // alert (n);
                //tab.deleteRow(j);
                tab.removeChild(enfant[j]);
            }

            /*   var lignes = tab.getElementsByTagName("tr");
             // alert (lignes.length);
             
             if (lignes.length <=2 ){
             
             for (var i=1 ;i< lignes.length;i++) {
             tab.deleteRow (i);
             
             }
             }
             else {
             
             for (var i=1 ;i<= lignes.length;i++) {
             tab.deleteRow (i);
             
             }
             
             tab.deleteRow(1);
             
             } */

            for (var i = 0; i < listeDEleves.length; i++) {
                //
                myNewTR = document.createElement("tr");
                myNewTR.setAttribute("class", "ligneQuestion");

                //var tdId = myNewTR.appendChild(document.createElement("td"));
                var tdNom = myNewTR.appendChild(document.createElement("td"));
                var tdPrenom = myNewTR.appendChild(document.createElement("td"));
                var tdAjouter = myNewTR.appendChild(document.createElement("td"));

                //tdId.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue));
                tdNom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("nom")[0].childNodes[0].nodeValue));
                tdNom.setAttribute('style', 'font-size: 20px;');
                tdPrenom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("prenom")[0].childNodes[0].nodeValue));
                tdPrenom.setAttribute('style', 'font-size: 20px;');

                identifiantGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                idGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                var ancreImgAjouter = tdAjouter.appendChild(document.createElement("a"));
                ancreImgAjouter.setAttribute('href', '../partie_generateur/ens_gestion_eleves.html');
                ancreImgAjouter.setAttribute('onclick', 'ajouterEleve(' + listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue + ')');
                var img = ancreImgAjouter.appendChild(document.createElement("img"));
                img.setAttribute('src', '../images/add.png');
                img.setAttribute('style', 'width: 50px; height: 50px;');

                tab.appendChild(myNewTR);
            }


        }







        function recupererListeEleves() {
            var listeDesEleves; // = recupererListeEleves() ;
            var xhr = getXMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                    alert("[" + xhr.responseText + "]");
                    //callback(xhr.responseText);
                    var docXML = xhr.responseText;
                    listeDesEleves = stringToXML(docXML);
                    construireTableauEleves(listeDesEleves);
                }
            };

            var fd = new FormData();
            fd.append("id", readCookie("Syba_id_personne"));

            xhr.open("POST", "../scripts_php/eleve_recuperer_tous_mes_mysql.php", false);
            xhr.send(fd);
        }

        function construireTableauEleves(xml) {
            //
            var tab = document.getElementById("tabEleves");
            var listeDEleves = xml.getElementsByTagName("eleve");
             var body = document.getElementsByTagName('article')[0];

            while (body.firstChild) {
                body.removeChild(body.firstChild);
              }

            for (var i = 0; i < listeDEleves.length; i++) {
           
                myNewTR = document.createElement("tr");
                myNewTR.setAttribute("class", "ligneQuestion");

                //var tdId = myNewTR.appendChild(document.createElement("td"));
                var tdNom = myNewTR.appendChild(document.createElement("td"));
                var tdPrenom = myNewTR.appendChild(document.createElement("td"));
                var tdModifier = myNewTR.appendChild(document.createElement("td"));
                var tdSupprimer = myNewTR.appendChild(document.createElement("td"));

                //tdId.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue));
                tdNom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("nom")[0].childNodes[0].nodeValue));
                tdNom.setAttribute('style', 'font-size: 20px;');
                tdPrenom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("prenom")[0].childNodes[0].nodeValue));
                tdPrenom.setAttribute('style', 'font-size: 20px;');

                identifiantGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                idGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                var ancreImgModifier = tdModifier.appendChild(document.createElement("a"));
                ancreImgModifier.setAttribute('href', '../partie_generateur/ens_gestion_eleves_edition.html?id=' + idGet);
                ancreImgModifier.setAttribute('onclick', 'submit()');

                var img = ancreImgModifier.appendChild(document.createElement("img"));
                img.setAttribute('src', '../images/edit.png');
                img.setAttribute('style', 'width: 50px; height: 50px;');


                var ancreImgModifier2 = tdSupprimer.appendChild(document.createElement("a"));
                ancreImgModifier2.setAttribute('href', '../partie_generateur/ens_gestion_eleves.html');
                ancreImgModifier2.setAttribute('onclick', 'supprimerEleve(' + listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue + ')');

                var img2 = ancreImgModifier2.appendChild(document.createElement("img"));
                img2.setAttribute('src', '../images/delete.png');
                img2.setAttribute('style', 'width: 50px; height: 50px;');


                tab.appendChild(myNewTR);
            }
        
        
        
      
            
               
        }



        function recupererListeElevesN() {
            var listeDesElevesN; // = recupererListeEleves() ;
            var xhr = getXMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                    //alert("[" + xhr.responseText + "]");
                    //callback(xhr.responseText);
                    var docXML = xhr.responseText;
                    listeDesElevesN = stringToXML(docXML);
                    construireTableauElevesN(listeDesElevesN);
                }
            };

            var fd = new FormData();
            fd.append("id", readCookie("Syba_id_personne"));

            xhr.open("POST", "../scripts_php/eleve_recuperer_tous_Nmes_mysql.php", false);
            xhr.send(fd);
        }

        function construireTableauElevesN(xml) {
            //
            var tab = document.getElementById("tabElevesA");
            var listeDEleves = xml.getElementsByTagName("eleveA");

            for (var i = 0; i < listeDEleves.length; i++) {
                //
                myNewTR = document.createElement("tr");
                myNewTR.setAttribute("class", "ligneQuestion");

                //var tdId = myNewTR.appendChild(document.createElement("td"));
                var tdNom = myNewTR.appendChild(document.createElement("td"));
                var tdPrenom = myNewTR.appendChild(document.createElement("td"));
                var tdAjouter = myNewTR.appendChild(document.createElement("td"));

                //tdId.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue));
                tdNom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("nom")[0].childNodes[0].nodeValue));
                tdNom.setAttribute('style', 'font-size: 20px;');
                tdPrenom.appendChild(document.createTextNode(listeDEleves[i].getElementsByTagName("prenom")[0].childNodes[0].nodeValue));
                tdPrenom.setAttribute('style', 'font-size: 20px;');

                identifiantGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                idGet = listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue;

                var ancreImgAjouter = tdAjouter.appendChild(document.createElement("a"));
                ancreImgAjouter.setAttribute('href', '../partie_generateur/ens_gestion_eleves.html');
                ancreImgAjouter.setAttribute('onclick', 'ajouterEleve(' + listeDEleves[i].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue + ')');
                var img = ancreImgAjouter.appendChild(document.createElement("img"));
                img.setAttribute('src', '../images/add.png');
                img.setAttribute('style', 'width: 50px; height: 50px;');

                tab.appendChild(myNewTR);
            }
        }

        function ajouterEleve(id) {
            var xhr = getXMLHttpRequest();

            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                    alert("[" + xhr.responseText + "]");
                    //callback(xhr.responseText);
                    var docXML = xhr.responseText;
                }
            };

            var fd = new FormData();
            fd.append("identifiant", id);
            fd.append("id", readCookie("Syba_id_personne"));

            xhr.open('POST', "../scripts_php/eleve_ajouterN_mysql.php", false);
            xhr.send(fd);
        }

        function supprimerEleve(id) {
            var r = confirm("Voulez vous supprimer cet utilisateur ? ");
            if (r === true)
            {
                //var currentLocation =  document.location.href;
                var xhr = getXMLHttpRequest();

                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        //alert("[" + xhr.responseText + "]");
                        //callback(xhr.responseText);
                        var docXML = xhr.responseText;
                    }
                };

                var fd = new FormData();
                fd.append("identifiant", id);
                fd.append("id", readCookie("Syba_id_personne"));

                xhr.open('POST', "../scripts_php/eleve_supprimer_mysql.php", false);
                xhr.send(fd);
            }

        }

        </script>

    </body>
</html>